package com.irdstudio.efp.riskm.api.rest;

import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.PubConstant;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.basic.sequence.service.facade.SequenceService;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.riskm.common.RiskColltConstant;
import com.irdstudio.efp.riskm.service.facade.CaseDistrRatioService;
import com.irdstudio.efp.riskm.service.facade.ColltTaskInfoService;
import com.irdstudio.efp.riskm.service.facade.CooprOrgInfoService;
import com.irdstudio.efp.riskm.service.vo.CaseDistrRatioVO;
import com.irdstudio.efp.riskm.service.vo.ColltTaskInfoVO;
import com.irdstudio.efp.riskm.service.vo.CooprOrgInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/api")
public class CooprOrgInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("cooprOrgInfoService")
	private CooprOrgInfoService cooprOrgInfoService;
	
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	@Autowired
	@Qualifier("dbSeqService")
	private SequenceService sequenceService;

	@Autowired
	@Qualifier("colltTaskInfoService")
	private ColltTaskInfoService colltTaskInfoService;

	@Autowired
	@Qualifier("caseDistrRatioService")
	private CaseDistrRatioService caseDistrRatioService;

	/**
	 * 委外机构管理列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/outs/coopr/org/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CooprOrgInfoVO>> queryOutsCooprOrgInfoAll(@RequestBody CooprOrgInfoVO vo) {
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		return getResponseData(this.getDataByRuleType(vo, dataRuleTypeMap));
	}

	/**
	 * 律师事务所管理列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/laws/coopr/org/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CooprOrgInfoVO>> queryLawsCooprOrgInfoAll(@RequestBody CooprOrgInfoVO vo) {
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		return getResponseData(this.getDataByRuleType(vo, dataRuleTypeMap));
	}

	/**
	 * 风险代理机构管理列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/agent/coopr/org/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CooprOrgInfoVO>> queryAgentCooprOrgInfoAll(@RequestBody CooprOrgInfoVO vo) {
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		return getResponseData(this.getDataByRuleType(vo, dataRuleTypeMap));
	}

	private List<CooprOrgInfoVO> getDataByRuleType(CooprOrgInfoVO vo, Map<String,String> dataRuleTypeMap){
		List<CooprOrgInfoVO> outputVo = null;
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
			case Owner:
				outputVo = cooprOrgInfoService.queryAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = cooprOrgInfoService.queryAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = cooprOrgInfoService.queryAllCurrDownOrg(vo);
				break;
			default:
				outputVo = cooprOrgInfoService.queryAllOwner(vo);
				break;
		}
		return outputVo;
	}



	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/coopr/org/info", method=RequestMethod.GET)
	public @ResponseBody ResponseData<CooprOrgInfoVO> queryByPk() {		
		CooprOrgInfoVO inVo = new CooprOrgInfoVO();
		CooprOrgInfoVO outputVo = cooprOrgInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param cooprOrgInfo
	 * @return
	 */
	@RequestMapping(value="/coopr/org/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CooprOrgInfoVO inCooprOrgInfoVo) {		
		int outputVo = cooprOrgInfoService.deleteByPk(inCooprOrgInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inCooprOrgInfoVo
	 * @return
	 */
	@RequestMapping(value="/coopr/org/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CooprOrgInfoVO inCooprOrgInfoVo) {
		setUserInfoToVO(inCooprOrgInfoVo);
		if (RiskColltConstant.COOPR_STS_02.equals(inCooprOrgInfoVo.getCooprStatus()) ||
				RiskColltConstant.COOPR_STS_03.equals(inCooprOrgInfoVo.getCooprStatus())) {
			//若是暂停或者终止则判断机构下是否会有案件和分按比例
			boolean isCases = checkIsCases(inCooprOrgInfoVo.getCooprOrgNo());
			boolean isCaseDistrRatio = checkIsCaseDistrRatio(inCooprOrgInfoVo.getCooprOrgNo());
			if (isCases) return ResponseData.create(null, "-1","该机构下存在处理中的案件，不允许修改为终止或者暂停状态！",false);
			if (isCaseDistrRatio) return ResponseData.create(null, "-2","该机构下存在有效的分按比例，不允许修改为终止或者暂停状态！",false);
		}
		inCooprOrgInfoVo.setLastUpdateUser(inCooprOrgInfoVo.getLoginUserId());
		inCooprOrgInfoVo.setLastUpdateOrg(inCooprOrgInfoVo.getLoginUserOrgCode());
		int outputVo = cooprOrgInfoService.updateByPk(inCooprOrgInfoVo);
		return getResponseData(outputVo);
	}

	private boolean checkIsCaseDistrRatio(String cooprOrgNo) {
		List<CaseDistrRatioVO> caseDistrRatioVO = caseDistrRatioService.queryAllByCoop(cooprOrgNo);
		return Objects.nonNull(caseDistrRatioVO)?true:false;
	}

	private boolean checkIsCases(String cooprOrgNo) {
		List<ColltTaskInfoVO> colltTaskInfoVO = colltTaskInfoService.queryAllByCoop(cooprOrgNo);
		return Objects.nonNull(colltTaskInfoVO)?true:false;
	}

	/**
	 * 委外机构新增数据
	 * @param inCooprOrgInfoVo
	 * @return
	 */
	@RequestMapping(value="/coopr/org/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCooprOrgInfo(@RequestBody CooprOrgInfoVO inCooprOrgInfoVo) {
//		try {
//			inCooprOrgInfoVo.setCooprOrgNo(sequenceService.getSequence("OUT_SOURCE_ORG_SEQ", null, null));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		inCooprOrgInfoVo.setInputTime(DateUtility.getCurrAppDateStr());
//
//		setUserInfoToVO(inCooprOrgInfoVo);
//		inCooprOrgInfoVo.setInputUserId(inCooprOrgInfoVo.getLoginUserId());
//		inCooprOrgInfoVo.setInputOrg(inCooprOrgInfoVo.getLoginUserOrgCode());
		int outputVo = cooprOrgInfoService.insertCooprOrgInfo(inCooprOrgInfoVo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/coopr/org/pop", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CooprOrgInfoVO>> queryCooprOrgAll(@RequestBody CooprOrgInfoVO vo) {
		List<CooprOrgInfoVO> outputVo = null;
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		outputVo = cooprOrgInfoService.queryAllCooprOrgByPage(vo);

		return getResponseData(outputVo);
		
	}
	
	/**
	 * 律师事务所新增数据
	 * @param inCooprOrgInfoVo
	 * @return
	 * @author liuty
	 * @date 2019年9月5日 下午2:55:55
	 * @version 0.1
	 */
	@RequestMapping(value="/law/org/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertLawOrgInfo(@RequestBody CooprOrgInfoVO inCooprOrgInfoVo) {
//		try {
//			inCooprOrgInfoVo.setCooprOrgNo(sequenceService.getSequence("LAW_FIRM_ORG_SEQ", null, null));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		inCooprOrgInfoVo.setInputTime(DateUtility.getCurrAppDateStr());
//
//		setUserInfoToVO(inCooprOrgInfoVo);
//		inCooprOrgInfoVo.setInputUserId(inCooprOrgInfoVo.getLoginUserId());
//		inCooprOrgInfoVo.setInputOrg(inCooprOrgInfoVo.getLoginUserOrgCode());
		int outputVo = cooprOrgInfoService.insertCooprOrgInfo(inCooprOrgInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 风险代理机构新增数据
	 * @param inCooprOrgInfoVo
	 * @return
	 * @author liuty
	 * @date 2019年9月5日 下午2:55:55
	 * @version 0.1
	 */
	@RequestMapping(value="/risk/org/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertRiskOrgInfo(@RequestBody CooprOrgInfoVO inCooprOrgInfoVo) {
//		try {
//			inCooprOrgInfoVo.setCooprOrgNo(sequenceService.getSequence("RISK_ORG_SEQ", null, null));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		setUserInfoToVO(inCooprOrgInfoVo);
//		inCooprOrgInfoVo.setInputTime(DateUtility.getCurrAppDateStr());
//		inCooprOrgInfoVo.setInputUserId(inCooprOrgInfoVo.getLoginUserId());
//		inCooprOrgInfoVo.setInputOrg(inCooprOrgInfoVo.getLoginUserOrgCode());
		int outputVo = cooprOrgInfoService.insertCooprOrgInfo(inCooprOrgInfoVo);
		return getResponseData(outputVo);
		
	}

	/**
	 * 获取风险代理机构编号
	 * @return
	 */
	@RequestMapping(value="/risk/org/no", method=RequestMethod.GET)
	public @ResponseBody ResponseData<String> getRiskOrgNo() {
		String riskOrgNo = null;
		try {
			riskOrgNo = sequenceService.getSequence("RISK_ORG_SEQ", null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getResponseData(riskOrgNo);
	}

	/**
	 * 获取律师事务所编号
	 * @return
	 */
	@RequestMapping(value="/law/firm/risk/org/no", method=RequestMethod.GET)
	public @ResponseBody ResponseData<String> getLawFirmOrgNo() {
		String lawFirmOrgNo = null;
		try {
			lawFirmOrgNo = sequenceService.getSequence("LAW_FIRM_ORG_SEQ", null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getResponseData(lawFirmOrgNo);
	}

	/**
	 * 获取律师事务所编号
	 * @return
	 */
	@RequestMapping(value="/outs/org/no", method=RequestMethod.GET)
	public @ResponseBody ResponseData<String> getOutsOrgNo() {
		String outsOrgNo = null;
		try {
			outsOrgNo = sequenceService.getSequence("OUT_SOURCE_ORG_SEQ", null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getResponseData(outsOrgNo);
	}

}
